The present invention relates to a method for generating a compilation correctly and efficiently in a programming language processing system for generating an object program by compiling a source program.
A method for generating object codes for controlling a DO statement in a FORTRAN program is generally classified into two groups, namely, a loop length calculating method shown in FIG. 1 and a loop control variable addition method shown in FIG. 2. Either group is generally employed in this type of compiler. The DO statement used herein has the form of DO s i=a,b,c in which: i represents a DO-variable; a represents an initial parameter; b represents a terminal parameter; and c represents an incrementation parameter. In the case where the incrementation parameter c for the loop control variable in the loop control variable calculating method shown in FIG. 2 is identified to be positive, it can be simplified as shown in FIG. 3 (in the case where the incrementation parameter c is negative, it can be simplified in the same manner except that the inequality sign in the step 11a in FIG. 3 is reversed.
In FIGS. 1, 2 and 3, the step 5 represents a process for calculating the loop iteration count, the step 6 represents a process for performing branching on the basis of a judgment as to whether the iteration count is zero or not, the step 8 represents a process for subtracting the value of 1 from the iteration count, the step 9 represents a process for substituting the initial parameter into the DO-variable, the step 10 represents a process for performing branching on the basis of a judgment as to whether the incrementation parameter is positive or negative, each of the steps 11a and 11b represents a process for performing branching on the basis of a result of comparison between the DO-variable and the terminal parameter, and the step 12 represents a process for adding the incrementation parameter to the DO-variable.
The respective operations of the aforementioned two methods will be described hereunder. In the loop length calculating method in FIG. 1, the iteration count K is calculated in the step 5 just before the loop. The result of the calculation is generally stored in a register for the purpose of executing performance efficiency. Then, a judgment is made in the step 6 as to whether the iteration count K is zero or not. When the iteration count is zero, the loop is terminated. Otherwise, the loop body is processed in the step 7 and then the iteration count K is decreased by 1 in the step 8 before the controlling is returned to the judgment of the step 6. That is, this method is a method in which the loop is executed by iterating the processing of the loop body unless the iteration count calculated just before the loop and then renewed so as to be decreased by 1 for each execution of the processing of the loop body reaches zero.
For example, this loop length calculating method is described in HITAC Program Product VOS 3 Optimization FORTRAN 77 OFORT 77E2, HAP FORTRAN 77 Language, Hitachi Ltd., pp. 58-61.
In the loop control variable addition method in FIG. 2, the initial parameter a is substituted into the DO-variable i first in the step 9. The DO-variable i is generally stored in a register for the purpose of executing performance efficiency. Then, a judgment is made in the step 10 as to whether the incrementation parameter c is positive or not. The step 10 branches to the step 11a or to the step 11b on the basis of the result of the judgment. When the judgment of the step 11a proves that the DO-variable i is larger than the terminal parameter b or when the judgment of the step 11b proves that the DO-variable i is smaller than the terminal parameter b, the loop is terminated. Otherwise, the loop body is processed in the step 7 and then the incrementation parameter c is added to the DO-variable i in the step 12 before control is returned to the judgment of the step 10. That is, this method is a method in which the loop is executed by iterating the processing of the loop body unless the DO-variable i set to the initial parameter a and then renewed so as to be increased by the incrementation parameter c exceeds the terminal parameter b.
For example, this loop control variable addition method is described in HP 9000 series 300 computer FORTRAN/9000 REFERENCE, Yokogawa-Hewlett-Packard, Ltd., pp. 4-42 to 4-43.
On the other hand, M. Nakanishi and Y. Ohno, "Easy Compiler Generating Method", Kyoritsu Shuppan Co., Ltd., pp. 156-159 describes the use of the aforementioned two generating methods on the basis of grammatical interpretation of FOR statement (which can be interpreted in the same manner as DO statement).
In the case of use of the loop length calculating method in FIG. 1, one register in the inside of a processor for executing the object program should be used wastefully for storing the iteration count. There arises a problem in that the processor may be disturbed from generating efficient codes. In the case of use of the loop control variable addition method in FIG. 2, the judging step 10 for determining whether the step 11a or the step 11b is to be used as the loop end judging step as shown in FIG. 2 is required when the sign of the incrementation parameter is indefinable at the time of compile. There arises not only a problem in that the quantity of object codes is increased but a problem in that a wasteful conditional judging process must be carried out. In practice, the wasteful conditional branching may bring a tendency to increase pipeline stalling so that executing performance may become poor. Moreover, in use of this method, there arises a disadvantage in that the iteration count may unsatisfy the ANSI standard because of the influence of rounding error in real-number calculation when the DO-variable is of a real type.
According to the ANSI standard, the iteration count for a DO-loop with an initial parameter a, a terminal parameter b and an incrementation parameter c is defined by the following expression: EQU MAX(INT((b-a+c)/c), 0)
That is, the true iteration count by which the loop is really iterated may be different from the iteration count representedby the calculation expression according to the ANSI standard. Accordingly, the compiler according to the ANSI standard cannot be said to be an exact compiler.
Considering the aforementioned problems, it may be preferable that a program to be compiled is compiled while the loop length calculating method and the loop control variable addition method are suitably alternatively selected corresponding to the type of the program.